<!--
 * @Author: Wayne 13235388225@163.com
 * @Date: 2023-10-13 11:19:25
 * @LastEditors: Wayne 13235388225@163.com
 * @LastEditTime: 2023-10-13 16:20:11
 * @FilePath: \Jkc-app-front-new\src\views\questionManage\questionDetail.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <div class="app-container">
    <div class="view-title">常见问题</div>

    <el-button
      type="primary"
      size="mini"
      @click="handlerAdd"
      v-permission="['questionDetail:add']"
      >新增</el-button
    >

    <el-table
      :data="detailTableData"
      v-loading="loading"
      element-loading-text="正在加载中"
      :header-cell-style="{
        borderColor: '#F0F0F0',
        height: '51px',
        background: '#F0F0F0',
        color: '#333',
      }"
      :row-style="{ height: '51px' }"
      :cell-style="{ borderColor: '#F0F0F0' }"
      style="width: 100%; margin-top: 20px"
    >
      <el-table-column prop="title" label="问题题目" align="center">
      </el-table-column>
      <el-table-column
        prop="detail"
        label="问题详情"
        show-overflow-tooltip
        align="center"
      >
      </el-table-column>
      <el-table-column prop="type" label="问题类型" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.type == 1">返费</span>
          <span v-else-if="scope.row.type == 3">激活</span>
          <span v-else-if="scope.row.type == 4">咨询</span>
          <span v-else-if="scope.row.type == 5">分销赚钱</span>
        </template>
      </el-table-column>
      <el-table-column prop="created_at" label="创建时间" align="center">
      </el-table-column>
      <el-table-column prop="updated_at" label="更新时间" align="center">
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="100" align="center">
        <template slot-scope="scope">
          <el-button
            @click="handleEdit(scope.row)"
            type="text"
            size="small"
            v-permission="['questionDetail:edit']"
            >修改</el-button
          >
          <el-button
            type="text"
            size="small"
            @click="handlerDelete(scope.row)"
            v-permission="['questionDetail:delete']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <div class="page_box">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNow"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>

    <!-- 添加问题弹窗 -->
    <el-dialog
      :title="dialogTitle"
      :visible.sync="addQuestionDialogVisible"
      width="35%"
      @close="handleCloseDialog"
    >
      <div class="dialog_add_box">
        <div class="add_title_box">
          <div class="add_title">问题标题：</div>
          <div>
            <el-input
              placeholder="请输入问题标题"
              v-model="questionTitle"
            ></el-input>
          </div>
        </div>

        <div class="add_title_box">
          <div class="add_title">问题内容：</div>
          <div>
            <!-- <el-input
              type="textarea"
              :rows="3"
              placeholder="请输入问题内容"
              v-model="questionDetail"
            >
            </el-input> -->
            <tinymce
              ref="myEditor"
              @input="input"
              :value="questionDetail"
            ></tinymce>
          </div>
        </div>

        <div class="add_title_box">
          <div class="add_title">问题类型：</div>
          <div>
            <el-select v-model="questionType" placeholder="请选择">
              <el-option
                v-for="item in typeOptions"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </div>
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancelDialogAddQuestion">取 消</el-button>
        <el-button type="primary" @click="sureDialogAddQuestion"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {
  getAppProblemsList,
  addAppProblems,
  updateAppProblems,
  deleteAppProblems,
  getAppProblemTypeList,
} from "@/api/questionManage.js";
import tinymce from "@/components/Tinymce/index.vue";
export default {
  components: {
    tinymce,
  },
  data() {
    return {
      pageNow: 1,
      pageSize: 10,
      total: null,
      detailTableData: [],
      addQuestionDialogVisible: false,
      questionTitle: "",
      questionDetail: "",
      questionType: "",
      typeOptions: [],
      editId: "",
      deleteId: "",
      loading: true,
      dialogTitle: "",
    };
  },
  mounted() {
    this.getAppProblemsList();
  },
  methods: {
    // 获取全部常见问题
    getAppProblemsList() {
      getAppProblemsList({
        page_now: this.pageNow,
        page_size: this.pageSize,
      }).then((res) => {
        if (res.code == 1) {
          this.loading = false;
          this.detailTableData = res.data;
          this.total = res.page_info.total_count;
        } else {
          this.loading = false;
          this.$message.error(res.msg);
          return;
        }
      });
    },

    // 获取全部问题类型
    getAppProblemTypeList() {
      getAppProblemTypeList({
        page_now: 1,
        page_size: 20,
      }).then((res) => {
        if (res.code == 1) {
          this.typeOptions = res.data;
        } else {
          this.$message.error(res.msg);
          return;
        }
      });
    },

    // 新增常见问题
    addAppProblems() {
      addAppProblems({
        title: this.questionTitle,
        detail: this.questionDetail,
        type: this.questionType,
      }).then((res) => {
        if (res.code == 1) {
          this.$message.success(res.msg);
          this.addQuestionDialogVisible = false;
          this.getAppProblemsList();
          this.questionTitle = "";
          this.questionDetail = "";
          this.questionType = "";
        } else {
          this.$message.error(res.msg);
          return;
        }
      });
    },

    // 修改问题详情
    updateAppProblems() {
      updateAppProblems({
        id: this.editId,
        title: this.questionTitle,
        detail: this.questionDetail,
        type: this.questionType,
      }).then((res) => {
        if (res.code == 1) {
          this.$message.success(res.msg);
          this.addQuestionDialogVisible = false;
          this.getAppProblemsList();
          this.questionTitle = "";
          this.questionDetail = "";
          this.questionType = "";
        } else {
          this.$message.error(res.msg);
          return;
        }
      });
    },

    // 删除问题
    deleteAppProblems() {
      deleteAppProblems({
        ids: this.deleteId,
      }).then((res) => {
        if (res.code == 1) {
          this.$message.success(res.msg);
          this.getAppProblemsList();
        } else {
          this.$message.error(res.msg);
          return;
        }
      });
    },

    input(val) {
      this.questionDetail = val;
    },

    // 添加弹窗-叉号
    handleCloseDialog() {
      this.addQuestionDialogVisible = false;
      this.questionTitle = "";
      this.questionDetail = "";
      this.questionType = "";
    },

    // 新增
    handlerAdd() {
      this.dialogTitle = "新增";
      this.getAppProblemTypeList();
      this.addQuestionDialogVisible = true;
    },

    // 新增弹窗确定
    sureDialogAddQuestion() {
      if (this.questionTitle == "") {
        this.$message.error("请输入问题标题");
        return;
      } else if (this.questionDetail == "") {
        this.$message.error("请输入问题内容");
        return;
      } else if (this.questionType == "") {
        this.$message.error("请选择问题类型");
        return;
      }
      if (this.editId) {
        this.updateAppProblems();
      } else {
        this.addAppProblems();
      }
    },

    // 新增弹窗取消
    cancelDialogAddQuestion() {
      this.addQuestionDialogVisible = false;
      this.questionTitle = "";
      this.questionDetail = "";
      this.questionType = "";
    },

    // 修改
    handleEdit(val) {
      this.dialogTitle = "编辑";
      this.getAppProblemTypeList();
      this.editId = val.id;
      this.questionTitle = val.title;
      this.questionDetail = val.detail;
      this.questionType = val.type;
      this.addQuestionDialogVisible = true;
    },

    // 删除
    handlerDelete(val) {
      this.deleteId = val.id;
      this.$confirm("是否确定删除该问题?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.deleteAppProblems();
        })
        .catch(() => {});
    },

    // 分页
    handleSizeChange(val) {
      this.pageSize = val;
      this.getAppProblemsList();
    },
    handleCurrentChange(val) {
      this.pageNow = val;
      this.pageSize = 10;
      this.getAppProblemsList();
    },
  },
};
</script>
<style lang="scss" scoped>
.add_box {
  padding-bottom: 10px;
}

.page_box {
  display: flex;
  justify-content: center;
  padding: 20px 0;
}

.dialog_add_box {
  .add_title_box {
    padding-top: 10px;

    .add_title {
      padding: 0 0 10px 0;
    }
  }
}
</style>
